<?php
// @author AliceXT
class CashbackCountModel extends Model{

	public $member_id;
	public $config;

	public function _initialize(){
		parent::_initialize();
	}
	// 返现余额转换为账户余额
	private function change($id = 0){
		$Member = M('member');
		$map['c.id'] = $id;
		$cashbackdata = $this->alias("c")
			->join("__CASHBACK_CONFIG__ as conf on c.product_id = conf.product_id" )
			->join("__MEMBER__ as m on m.id = c.member_id")
			->where($map)
			->field("c.site_id,c.id as id,c.rest as rest,c.rate as rate,conf.status,c.member_id,m.money")
			->select();
			
			
			
		foreach($cashbackdata as $v){
			$cashback = $v;
			$this->member_id = $cashback['member_id'];//记录返现用户id
			$conf_map['site_id'] = $cashback['site_id'];
			$this->config = M("Cashback_config")->where($conf_map)->find();
			// dump($cashback);
	
			$me_map['id'] = $cashback['member_id'];
			$c_map['id'] = $id;
			// 该元祖不存在或返现余额不达100或返现开关关闭
			if(empty($cashback) || $cashback['rest'] < 100){
				// echo "不满足提现条件";
				return false;
			}else{
				// 待返金额只取100元的倍数，如果待返金额为9999，那待返金额为9900元，
				// 那这一天的返现积分为9900乘于日返率。当返到9800-9899元时，就按9800来算，待返余额低于100元不再返积分
				$change_money = $cashback['rate'] * 0.01 * ($cashback['rest'] / 100 ) ;
				//echo $cashback['rate'].'*0.01*'.($cashback['rest'] / 100 );
				//dump($cashback['rate']);
				
				
				// dump($change_money);
				//$ca_result = $this->where($c_map)->setDec("rest",$change_money);
	            return $change_money;
				/*$me_result = $Member->where($me_map)->setInc("money",$change_money);
				
				
					// dump($me_result);
					// dump($ca_result);
				if($me_result && $ca_result){
					
				}else{
					// 回滚操作过的事件
					$me_result && $Member->where($me_map)->setDec("money",$change_money);
					$ca_result && $this->where($c_map)->setInc("rest",$change_money);
					return false;
				}*/
			}
		}
		
	}

	// 根据返现时间间隔进行日返率调整
	private function adjust($id = 0){
		return false;
		$c_map['id'] = $map['c.id'] = $id;
		$cashback = $this->alias("c")->join("__CASHBACK_CONFIG__ as conf on c.site_id = conf.site_id" )->where($map)
			->field("c.rate,c.last_adjust,conf.adjust_time")->select();
		$cashback = $cashback[0];
		// 计算相差的天数
		$days = abs(floor(time() / (24 * 3600)) - floor($cashback['last_adjust'] / (24 * 3600)));
		// 到调整时间且日返率大于万分之一
		if($days >= $cashback['adjust_time'] && $cashback['rate'] > 1){
			// 进行日返率调整
			$data['rate'] = $cashback['rate'] - 1;
			$data['last_adjust'] = time();
			return $this->where($c_map)->setField($data);
		}else{
			return false;
		}
	}
	
	// 批量返现金操作
	public function cashbackByDay(){
		// 选出所有可以返现的元祖
		// 选择返现余额大于100的
		$map['c.rest'] = array("egt",100);
		// 选择站点配置为开的
		//$map['conf.status'] = 1;

		//$counts = $this->alias("c")->join("__CASHBACK_CONFIG__ as conf on c.product_id = conf.product_id")->where($map)->field("c.id")->select();
		$counts = M('Cashback_count') -> where(array('rest' => array('gt', 0))) -> select();
		//dump($counts);
		// echo "<br>".$this->getLastSql();
		// 实例日志模型
		$Log = D("Member_account_log");
		// 对每条元祖进行返现
		foreach($counts as $count){
			$id = $count['id'];
			// echo "<br>进入循环".$id;
			//返返金额
			$change_money = $count['total'] * 0.01 * ($count['rate'] / 100 );
			$product_title = M('Product') -> where(array('id' => $count['product_id'])) -> getField('title');
			
			$ca_result = $this->where(array('id' => $id))->setDec("rest",$change_money);
			//dump($count['member_id'].':'.$change_money);
			//dump($count['member_id'].'--'.$change_money);
			$Log->change($change_money,$count['member_id'],'购买商品【'.$count['product_id'].':'.$product_title.'】返现');//注意，这里remark一定要出现返现二字，不然可能搜索不到日志结果
			//if($change_money = $this->change($id)){
				 
				
				// echo "成功对返现".$this->member_id."共".$this->config['remark'];
				//$Log->change($change_money,$this->member_id,$this->config['remark']);//注意，这里remark一定要出现返现二字，不然可能搜索不到日志结果
			//}
		}
	}

	// 批量日返率调整
	public function adjustByDay(){
		// 选出所有可调整元祖
		$map['rate'] = array("gt",1);

		$counts = $this->where($map)->select();
		// 对每条元祖进行调整
		foreach($counts as $count){
			$id = $count['id'];
			$this->adjust($id);
		}
	}
}